﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Basic.Part2.Configuration
{
    /// <summary>
    /// 表示当前 common block 的配置节组
    /// </summary>
    class ConfigurationSourceGroup : ConfigurationSectionGroup, IConfigurationSource
    {
        private const string ObjectBuilderItem = "context";
        //public ConfigurationSourceGroup() : base() { }

        /// <summary>
        /// 用于动态生成 IObjectBuilder 的配置节
        /// </summary>
        [ConfigurationProperty(ObjectBuilderItem, IsRequired = true)]
        public ObjectBuilderConfigurationSource ObjectBuilder => Sections[ObjectBuilderItem] as ObjectBuilderConfigurationSource;


        public void Load()
        {
            var modeltype = System.Type.GetType(ObjectBuilder.TypeName);
            if (modeltype == null) throw new NullReferenceException();
            var interfaceType = modeltype.GetInterface($"I{modeltype.Name}");
            ConfigurationBroker.Add(interfaceType, Activator.CreateInstance(modeltype));
        }
    }
}
